import express from 'express';
import * as user from '../service/user';
import * as sms from '../libs/aliSms';
import request from 'request';
// 校验文件以及API文档
const Route = express.Router();

Route.route('/user').post(async (req, res) => {
	try {
		const count = await user.select({data: req.body});
		console.log(count.data)
		if (count.data.length) {
			baseResponse(req, res, { code: 2, message: '您已经填写过了，不需要重复提交~', data: count.data[0] });
			return;
		}
		const result = await user.add({ data: req.body });
		if (result.code !== 1) {
			baseResponse(req, res, { code: 4, message: '资源未找到~' });
		} else {
			// sms.sendSms({
			//    phone: req.body.phone,
			//    params: {
			// 	name: req.body.representative_name || req.body.name
			//    } 
			// })
			// const content = `【SNIEC客户答谢会】尊敬的${req.body.representative_name || req.body.name || ''}（先生/女士）您已成功报名参加2019年11月21日上海新国际博览中心“思事源·善若水”客户答谢晚宴。感谢您的支持和鼓励，期待您的莅临！地址:中国上海浦东新区浦明路988号（上海鲁能JW万豪侯爵酒店） 1楼荣爵厅`;
			// let smsUrl = `http://47.99.121.12:7862/sms?action=send&account=922118&password=MYF8z6&mobile=${req.body.phone}&content=${content}&extno=10690118&rt=json`
			// console.log(smsUrl)
			// request(smsUrl, (smsRes, response, body) => {
			// 	console.log('smsRes' + JSON.stringify(smsRes));
			// 	console.log('response' + JSON.stringify(response));
			// 	console.log('body' + JSON.stringify(body));
			// })
			baseResponse(req, res, {
				code: 1,
				message: '填写成功~',
				data: {
					id: result.data.insertId
				}
			});
		}
	} catch (error) {
		baseResponse(req, res, { code: -1, message: error });
		console.log(error);
	}
});

export default Route;
